governments_fault <- function(df, blame_variable, new_variable){

  blame_variable <- enquo(blame_variable)
  new_variable <- quo_name(enquo(new_variable))
  
  new_df <- df %>% mutate(!!new_variable := case_when(
      !!blame_variable == 111 ~ 0,
      !!blame_variable %in% c(0:10) ~ as.numeric(!!blame_variable),
      !!blame_variable == 99 ~ NA_real_,
      TRUE ~ NA_real_))

  return(new_df)
}

experiment_handle <- function(df, handling_variable, new_variable){

  handling_variable <- enquo(handling_variable)
  new_variable <- quo_name(enquo(new_variable))

  new_df <- df %>% mutate(!!new_variable := factor(case_when(
      !!handling_variable == 1    ~ "Very badly",
      !!handling_variable == 2    ~ "Quite badly",
      !!handling_variable == 3    ~ "Neither well nor badly",
      !!handling_variable == 4    ~ "Quite well",
      !!handling_variable == 5    ~ "Very well",
      !!handling_variable == 99 ~ NA_character_),
    levels = c("Very badly", 
               "Quite badly", 
               "Neither well nor badly", 
               "Quite well", 
               "Very well")))

  return(new_df)
}

experiment_handle_as_numbers <- function(df, handling_variable, new_variable){

  handling_variable <- enquo(handling_variable)
  new_variable <- quo_name(enquo(new_variable))

  new_df <- df %>% mutate(!!new_variable := case_when(
      !!handling_variable %in% c(1:5) ~ !!handling_variable,
      !!handling_variable == 9999 ~ NA_real_))

  return(new_df)
}

age_four_categories <- function(df, age_variable, new_variable_name){

  age_variable <- enquo(age_variable)
  new_variable_name <- quo_name(enquo(new_variable_name))

  new_df <- df %>% mutate(!!new_variable_name := factor(case_when(
      !!age_variable <= 29 ~ "18to29", # because no under 18s
      !!age_variable %in% c(30:44) ~ "30to44", 
      !!age_variable %in% c(45:59) ~ "45to59", 
      !!age_variable >= 60 ~ "60plus", 
      TRUE ~ NA_character_),
    levels = c("18to29",
               "30to44",
               "45to59",
               "60plus")))

  return(new_df)
}

class_seven_categories <- function(df, class_variable, new_variable_name){

  class_variable <- enquo(class_variable)
  new_variable_name <- quo_name(enquo(new_variable_name))

  new_df <- df %>% mutate(!!new_variable_name := fct_recode(
    as.factor(!!class_variable),
      "Higher Managerial" = "11",
      "Higher Managerial" = "12", # should be prof. but collapsed
      "Lower Professional" = "20",
      "Intermediate Occupations" = "30",
      "Small Employers" = "40",
      "Lower Supervisory" = "50",
      "Semi-routine Occupations" = "60",
      "Routine Occupations" = "70"))
  
  return(new_df)
}

white_dummy <- function(df, ethnicity_variable, new_variable_name){

  ethnicity_variable <- enquo(ethnicity_variable)
  new_variable_name <- quo_name(enquo(new_variable_name))

  new_df <- df %>% mutate(!!new_variable_name := case_when(
      !!ethnicity_variable == 1 ~ 1,
      !!ethnicity_variable == 16 ~ NA_real_,
      !is.na(!!ethnicity_variable) ~ 0,
      TRUE ~ NA_real_))

  return(new_df)
}

male_dummy <- function(df, gender_variable, new_variable_name){

  gender_variable <- enquo(gender_variable)
  new_variable_name <- quo_name(enquo(new_variable_name))

  new_df <- df %>% mutate(!!new_variable_name := case_when(
      !!gender_variable == 1 ~ 1,
      !!gender_variable == 2 ~ 0))

  return(new_df)
}

education_five_categories <- function(df, education_variable, new_variable_name){

  education_variable <- enquo(education_variable)
  new_variable_name <- quo_name(enquo(new_variable_name))

  new_df <- df %>% mutate(!!new_variable_name := factor(case_when(
      !!education_variable %in% c(4, 5) ~ "Undergrad or higher", 
      !!education_variable == 3 ~ "A-level", 
      !!education_variable == 2 ~ "GCSE", 
      !!education_variable == 1 ~ "Below GCSE", 
      !!education_variable == 0 ~ "No qualifications"),
    levels = c("Undergrad or higher",
               "A-level",
               "GCSE",
               "Below GCSE",
               "No qualifications")))
  
  return(new_df)
}

party_id_opposition_gov <- function(df, 
                                    party_id_var, 
                                    new_variable_name){

  party_id_var <- enquo(party_id_var)
  new_variable_name <- quo_name(enquo(new_variable_name))
  
  new_df <- df %>% mutate(!!new_variable_name := factor(case_when(
        !!party_id_var == 1 ~ "Government partisan",
        !!party_id_var %in% c(2:9, 11:12) ~ "Opposition partisan",
        !!party_id_var == 10 ~ "Non-partisan",
        !!party_id_var == 9999 ~ "Non-partisan"),
      levels = c("Opposition partisan", "Government partisan", "Non-partisan")))
  
  return(new_df)
}

handle_corona_to_factor <- function(df, handling_variable, new_variable){

  handling_variable <- enquo(handling_variable)
  new_variable <- quo_name(enquo(new_variable))

  new_df <- df %>% mutate(!!new_variable := factor(case_when(
      !!handling_variable == 1    ~ "Very badly",
      !!handling_variable == 2    ~ "Fairly badly",
      !!handling_variable == 3    ~ "Neither well nor badly",
      !!handling_variable == 4    ~ "Fairly well",
      !!handling_variable == 5    ~ "Very well",
      !!handling_variable == 9999 ~ NA_character_),
    levels = c("Very badly", 
               "Fairly badly", 
               "Neither well nor badly", 
               "Fairly well", 
               "Very well")))

  return(new_df)
}


